<?php
namespace app\pay\controller\juyin;

use phpmailer\Email as Email;

class Callback extends Base
{
    public function test() {
        Email::send('wowchky@163.com', '测试', '测试');
    }
    
    // 通知回调
    public function index() {
        $data = input('post.');     // 接收聚银异步通知POST过来的数据      
        $code = $data['code'];
        // Email::send('wowchky@163.com', '聚银异步通知数据', json_encode($data, true));
        // 检测订单号
        $ret = model('PayBisOrder')->get(['pay_orderid' => $data['orderno']]);
        if($ret) {
            $payAmount = $ret->pay_amount;
            // 检测交易金额
            if($code == '11' && $data['transamt'] == (string)$payAmount) {
                // 发送邮件
                Email::send('wowchky@163.com', '聚银异步通知数据', json_encode($data, true));
                
                // 更新支付状态
                $updateStatus = [
                    'payment_time' => time(),
                    'status'       => 1
                 ];
                model('PayBisOrder')->updateById($updateStatus, $ret->id);
                
                /*
                 * 更新商户可用余额 
                 */
                // 获取商户可用余额
                $res = model('PayBis')->get(['id' => $ret->merchant_id]);
                $amount = $res->amount;
                $payBisData = [
                    'amount' => $ret->actual_amount + $amount,
                ];
                // 更新商户可用余额 
                model('PayBis')->updateById($payBisData, $ret->merchant_id);
                
                /*
                 * 推送异步通知给客户端提供的回调地址
                 */           
                // 接收聚银返回的数据并组装
                $result = $this->responseData($ret->merchant_id, json_encode($data, true));
                // 消息队列
                \think\Queue::push('app\queue\jop\Index@fire', $result, $queue = 'payCallbackQueue');
                // 返回success字符串给聚银
                return 'success';
            }  
        }
    }
    
    // 接收聚银返回的数据并组装
    public function responseData($merchantId, $data) {
        $retData = json_decode($data, true);
        if($retData['code'] == '11') {
            // 数据组装
            $result = array(
                'code'          => $retData['code'],
                'transactionid' => $retData['transactionid'],
                'systemorderid' => $retData['systemorderid'],
                'orderno'       => $retData['orderno'],
                'bankaccount'   => $retData['bankaccount'],
                'merno'         => $merchantId,
                'transamt'      => $retData['transamt'],
                'timeend'       => $retData['timeend'],
                'banktype'      => $retData['banktype'],
                'productid'     => $retData['productid']
            );
            $this->request();
            // 生成新的签名
            $newJuyinSignVal = $this->payRequest->createJuyinSign($result, false);
            $result['signature'] = $newJuyinSignVal;
        } else {
            $result = array(
                'code' => $retData['code'],
                'msg'  => $retData['msg']
            );
        }
        return json_encode($result, true);
    }
      
    
    // 通知回调
    public function callback() {
        $data = input('post.');     // 接收聚银异步通知POST过来的数据      
        Email::send('wowchky@163.com', '通知3', json_encode($data, true));
        return '123';
    }
}

